package com.flyme.common.core.utils.jpush;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.audience.AudienceTarget;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;
import com.flyme.common.core.utils.ObjectUtils;

public class JpushPush {

	// 推送标题
	private String TITLE = "";
	// 推送标头，即上部提示框内容
	private String ALERT = "";
	// 推送内容
	private String MSG_CONTENT = "";
	// 设备标识id
	private String[] REGISTRATION_IDS;
	// 推送匹配标签
	private String[] TAGS;

	public JpushPush() {
	}
	public JpushPush(String title, String alert, String msg_content, String[] registration_ids, String[] tags, String alias) {
		super();
		TITLE = title;
		ALERT = alert;
		MSG_CONTENT = msg_content;
		REGISTRATION_IDS = registration_ids;
		TAGS = tags;
	}

	// 额外参数/附加字段，前边是key后边是value
	private String extraMap;

	public String getExtraMap() {
		return extraMap;
	}

	public void setExtraMap(String extraMap) {
		this.extraMap = extraMap;
	}

	// 全部推送alert
	public JpushReult pushAllAlert() {
		PushPayload payload = PushPayload.alertAll(ALERT);
		return pushOut(payload,"COMM");
	}

	// 全部推送message
	public JpushReult pushAllMessage() {
		PushPayload payload = PushPayload.messageAll(MSG_CONTENT);
		return pushOut(payload,"COMM");
	}

	// 全部推送
		public JpushReult pushALL(String userType, String alert, String msgType,  String info) {
			Map<String, String> map = new HashMap<String, String>();
			map.put("msgType", msgType);
			if (ObjectUtils.isNotEmpty(info)) {
				map.put("info", info);
			}
			PushPayload payload = PushPayload.newBuilder()
					.setPlatform(Platform.all())
					.setAudience(Audience.all())
					.setNotification(Notification.newBuilder()
							.addPlatformNotification(IosNotification.newBuilder().setAlert(alert).addExtras(map).setSound("default").build())
							.addPlatformNotification(AndroidNotification.newBuilder().setAlert(alert).addExtras(map).build())
							.build())
					.setOptions(Options.newBuilder().setApnsProduction(true)
							//.setOverrideMsgId(msgId)
							.build())
					.build();
			return pushOut(payload,userType);
		}

	
	// 根据alias推送
	public JpushReult pushByAlias(String userType, List<String> alias, String alert, String msgType, String infoId, Map<String, String> info) {
		Map<String, String> map = new HashMap<String, String>();
		map.put("msgType", msgType);
		map.put("id", infoId);
		if (ObjectUtils.isNotEmpty(info)) {
			map.putAll(info);
		}
		PushPayload payload = PushPayload.newBuilder()
				.setPlatform(Platform.all())
				.setAudience(Audience.newBuilder().addAudienceTarget(AudienceTarget.alias(alias)).build())
				.setNotification(Notification.newBuilder()
						.addPlatformNotification(IosNotification.newBuilder().setAlert(alert).addExtras(map).setSound("default").build())
						.addPlatformNotification(AndroidNotification.newBuilder().setAlert(alert).addExtras(map).build())
						.build())
				.setOptions(Options.newBuilder().setApnsProduction(true)
						//.setOverrideMsgId(msgId)
						.build())
				.build();
		return pushOut(payload,userType);
	}

	// 安卓alert,title（此项只有安卓独有）
	public JpushReult pushAndroidAlertTitle() {
		PushPayload payload = PushPayload.newBuilder().setPlatform(Platform.all()).setAudience(Audience.all()).setNotification(Notification.android(ALERT, TITLE, null)).build();
		return pushOut(payload,"COMM");
	}

	// ios,alert
	public JpushReult pushIosAlert() {
		PushPayload payload = PushPayload.newBuilder().setPlatform(Platform.all()).setAudience(Audience.all()).setNotification(Notification.ios(ALERT, null)).build();
		return pushOut(payload,"COMM");
	}

	// 按标签推送，1安卓，2ios，0全部，3wp
	public JpushReult pushByTag(int type) {
		JpushReult result = new JpushReult();
		if (ObjectUtils.isEmpty(ALERT)) {
			result.setReultMsg(PushStaticString.Push_Connection_Null_Error);
			return result;
		}
		PushPayload payload = null;
		switch (type) {
		case 1:
			payload = PushPayload.newBuilder().setPlatform(Platform.android()).setAudience(Audience.tag(TAGS)).setNotification(Notification.alert(ALERT))
					// .setMessage(
					// Message.newBuilder().setMsgContent(MSG_CONTENT)
					// .build())
					.build();
			break;

		case 2:
			payload = PushPayload.newBuilder().setPlatform(Platform.ios()).setAudience(Audience.tag(TAGS)).setNotification(Notification.alert(ALERT))
					// .setMessage(
					// Message.newBuilder().setMsgContent(MSG_CONTENT)
					// .build())
					.build();
			break;
		case 3:
			payload = PushPayload.newBuilder().setPlatform(Platform.winphone()).setAudience(Audience.tag(TAGS)).setNotification(Notification.alert(ALERT))
					// .setMessage(
					// Message.newBuilder().setMsgContent(MSG_CONTENT)
					// .build())
					.build();
			break;
		default:
			payload = PushPayload.newBuilder().setPlatform(Platform.all()).setAudience(Audience.tag(TAGS)).setNotification(Notification.alert(ALERT))
					// .setMessage(
					// Message.newBuilder().setMsgContent(MSG_CONTENT)
					// .build())
					.build();
			break;
		}
		return pushOut(payload,"COMM");
	}

	// 按设备id推送，1安卓，2ios，0全部，3wp
	public JpushReult pushByIds(int type) {
		JpushReult result = new JpushReult();
		if (ObjectUtils.isEmpty(ALERT)) {
			result.setReultMsg(PushStaticString.Push_Connection_Null_Error);
			return result;
		}
		PushPayload payload = null;
		switch (type) {
		case 1:
			payload = PushPayload.newBuilder().setPlatform(Platform.android()).setAudience(Audience.registrationId(REGISTRATION_IDS)).setNotification(Notification.alert(ALERT))
					// .setMessage(
					// Message.newBuilder().setMsgContent(MSG_CONTENT)
					// .build())
					.build();
			break;

		case 2:
			payload = PushPayload.newBuilder().setPlatform(Platform.ios()).setAudience(Audience.registrationId(REGISTRATION_IDS)).setNotification(Notification.alert(ALERT))
					// .setMessage(
					// Message.newBuilder().setMsgContent(MSG_CONTENT)
					// .build())
					.build();
			break;
		case 3:
			payload = PushPayload.newBuilder().setPlatform(Platform.winphone()).setNotification(Notification.alert(ALERT)).setAudience(Audience.registrationId(REGISTRATION_IDS))
					// .setMessage(
					// Message.newBuilder().setMsgContent(MSG_CONTENT)
					// .build())
					.build();
			break;
		default:
			payload = PushPayload.newBuilder().setPlatform(Platform.all()).setAudience(Audience.registrationId(REGISTRATION_IDS)).setNotification(Notification.alert(ALERT))
					// .setMessage(
					// Message.newBuilder().setMsgContent(MSG_CONTENT)
					// .build())
					.build();
			break;
		}
		return pushOut(payload,"COMM");
	}

	private JpushReult pushOut(PushPayload payload,String type) {
		JPushClient jpushClient = new JPushClient(PushStaticString.masterSecretComm, PushStaticString.appKeyComm);;
		JpushReult result = new JpushReult();
		try {
			result.setResult(jpushClient.sendPush(payload));
		} catch (APIConnectionException e) {
			e.printStackTrace();
		} catch (APIRequestException e) {
			e.printStackTrace();
		}
		result.setReultMsg(PushStaticString.Push_Success);
		return result;
	}
}
